package cn.xiuxius.askbox.controller.console;


import cn.xiuxius.askbox.common.constant.DataState;
import cn.xiuxius.askbox.pojo.dto.update.AskUpdateStateDTO;
import cn.xiuxius.askbox.pojo.dto.query.AskQueryDTO;
import cn.xiuxius.askbox.pojo.dto.update.AskUpdateDTO;
import cn.xiuxius.askbox.pojo.result.PageResult;
import cn.xiuxius.askbox.pojo.result.Result;
import cn.xiuxius.askbox.pojo.vo.AskVO;
import cn.xiuxius.askbox.service.IAskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoxiu
 * @since 2024-07-20
 */
@Api(tags = "后台-提问接口")
@RestController("consoleAskController")
@RequestMapping("/console/asks")
public class AskController {


    @Autowired
    private IAskService askService;

    @ApiOperation("分页查询所有提问")
    @GetMapping
    public Result getAsksByPage(@Validated AskQueryDTO dto) {
        //设置为有效数据
        dto.setState(DataState.ALIVE);
        PageResult result = askService.getAsksByPage(dto);
        return Result.success(result);
    }


    @ApiOperation("根据问题uuid查询问题")
    @GetMapping("/{uuid}")
    public Result getAskByAskUuid(@PathVariable String uuid) {
        AskQueryDTO dto = new AskQueryDTO();
        //设置为有效数据
        dto.setState(DataState.ALIVE);
        dto.setUuid(uuid);
        AskVO result = askService.getAskByAskUuid(dto);
        return Result.success(result);
    }

    @ApiOperation("根据提问uuid修改提问")
    @PutMapping
    public Result updateAskByAskUuid(@Validated @RequestBody AskUpdateDTO dto) {
        askService.updateAskByAskUuid(dto);
        return Result.success();
    }

    @ApiOperation("根据提问uuid更新提问状态")
    @PutMapping("/state")
    public Result updateStateAskByAskUuid(@Validated @RequestBody AskUpdateStateDTO dto) {
        askService.updateStateAskByAskUuid(dto);
        return Result.success();
    }

    @ApiOperation("根据问题uuid删除问题")
    @DeleteMapping("/{uuid}")
    public Result deleteAskByAskUuid(@PathVariable String uuid) {
        askService.deleteAskByAskUuid(uuid);
        return Result.success();
    }

    @ApiOperation("分页查询回收站的提问")
    @GetMapping("/deleted")
    public Result getDeletedAsksByPage(@Validated AskQueryDTO dto) {
        dto.setState(DataState.DELETED);
        PageResult pageResult = askService.getAsksByPage(dto);
        return Result.success(pageResult);
    }

    @ApiOperation("根据问题uuid查询已删除的问题")
    @GetMapping("/deleted/{uuid}")
    public Result getDeletedAskByAskUuid(@PathVariable String uuid) {
        AskQueryDTO dto = new AskQueryDTO();
        //设置为已删除的数据
        dto.setState(DataState.DELETED);
        dto.setUuid(uuid);
        AskVO result = askService.getAskByAskUuid(dto);
        return Result.success(result);
    }

}
